package com.hmh.student.students.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmh.student.students.entity.Course;
import com.hmh.student.students.entity.StudentCourse;
import com.hmh.student.students.mapper.StudentCourseMapper;
import com.hmh.student.students.service.ICourseService;
import com.hmh.student.students.service.IStudentCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmh.student.students.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xyf
 * @since 2025-04-16
 */
@Service
public class StudentCourseServiceImpl extends ServiceImpl<StudentCourseMapper, StudentCourse> implements IStudentCourseService {




    @Override
    public Page<StudentCourse> queryStudentCoursePage(Integer studentNo, Integer courseNo, Integer pageSize, Integer current) {
        QueryWrapper<StudentCourse> queryWrapper = new QueryWrapper<>();

        if (studentNo != null) {
            queryWrapper.like("student_no", studentNo);
        }

        if (courseNo != null) {
            queryWrapper.like("course_no", courseNo);
        }

        Page<StudentCourse> page = new Page<>(current, pageSize);
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Integer> getCourseByStudentId(Integer studentNo) {
        LambdaQueryWrapper<StudentCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentCourse::getStudentNo, studentNo);
        return this.list(queryWrapper).stream()
                .map(StudentCourse::getCourseNo)
                .collect(Collectors.toList());

    }

    @Override
    public boolean assignCourses(Integer studentNo, List<Integer> courseNo) {
        LambdaQueryWrapper<StudentCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentCourse::getStudentNo, studentNo);
        this.remove(queryWrapper);

        if (courseNo !=null && !courseNo.isEmpty()) {
            List<StudentCourse> studentCourses = courseNo.stream()
                    .map(courseNos ->{
                        StudentCourse studentCourse = new StudentCourse();
                        studentCourse.setStudentNo(studentNo);
                        studentCourse.setCourseNo(courseNos);
                        return studentCourse;
                    })
                    .collect(Collectors.toList());
         return this.saveBatch(studentCourses);
        }
        return false;

    }




}
